Ext.define("core.jbpm.controller.WfDeployController", {
	extend : "Ext.app.Controller",
	init : function() {
		var self = this;
		this.control({
			"deploygrid button[ref=addDeploy]" : {
				click : function(btn) {
					var grid = btn.up("deploygrid");
					var store = grid.getStore();
					var records = grid.getSelectionModel().getSelection();
					if (records == null || records.length <= 0) {
						Ext.Msg.alert("提示", "请选择数据");
					}
					var obj = records[0];
					Ext.Ajax.request({
						url : APPBASE + "pc/wfDeploymentAction!deployment.action",
						params : obj.data,
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.decode(response.responseText);
							if (resObj.success) {
								store.load();
								var funGrid = grid.up("centerview").down("processgrid");
								if (funGrid != null) {
									funGrid.getStore().load();
								}
								Ext.Msg.alert("提示", resObj.obj);
							} else {
								Ext.Msg.alert("提示", resObj.obj);
							}
						}
					});
				}
			},
			"deploygrid button[ref=deleteDeploy]" : {
				click : function(btn) {
					var grid = btn.up("deploygrid");
					var store = grid.getStore();
					var records = grid.getSelectionModel().getSelection();
					if (records == null || records.length <= 0) {
						Ext.Msg.alert("提示", "请选择数据");
					}
					var obj = records[0];
					Ext.MessageBox.confirm('确认要删除流程部署吗?删除将不能恢复!', mess, function(btn) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : APPBASE + "pc/wfDeploymentAction!deleteDeploy.action",
								params : obj.data,
								method : "POST",
								timeout : 4000,
								success : function(response, opts) {
									var resObj = Ext.decode(response.responseText);
									if (resObj.success) {
										store.load();
										var funGrid = grid.up("centerview").down("processgrid");
										if (funGrid != null) {
											funGrid.getStore().load();
										}
										Ext.Msg.alert("提示", resObj.obj);
									} else {
										Ext.Msg.alert("提示", resObj.obj);
									}
								}
							});
						}
					});
				}
			},
			"deploygrid button[ref=calDeploy]" : {
				click : function(btn) {
					var grid = btn.up("deploygrid");
					var store = grid.getStore();
					var records = grid.getSelectionModel().getSelection();
					if (records == null || records.length <= 0) {
						Ext.Msg.alert("提示", "请选择数据");
					}
					var obj = records[0];
					Ext.Ajax.request({
						url : APPBASE + "pc/wfDeploymentAction!clearDeployment.action",
						params : obj.data,
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.decode(response.responseText);
							if (resObj.success) {
								store.load();
								var funGrid = grid.up("centerview").down("processgrid");
								if (funGrid != null) {
									funGrid.getStore().load();
								}
								Ext.Msg.alert("提示", resObj.obj);
							} else {
								Ext.Msg.alert("提示", resObj.obj);
							}
						}
					});
				}
			}, //上传工作流
			"deploygrid button[ref=uploadDeploy]" : {
				click : function(btn) {
					new Ext.window.Window({
						width : 650,
						autoScroll:true,
						title : '工作流文件上传(<font color="red">请选择x.jpdl.xml与x.png文件上传</font>)',
						height : 300,
						region : 'center',
						layout : 'fit',
						items: [{ 
				            xtype: 'uploadFileForm'
			        	}]
					}).show();
				}
			}, //上传工作流--添加文件
			"uploadFileForm button[ref=uploadFileAddButton]" : {
				click : function(btn) {
					var form =btn.up("uploadFileForm");
					var panel = new Ext.Panel({
							layout : 'column',
							items: [{ 
					            xtype: 'filefield', 
					            name: 'image', 
					            labelAlign : 'right',
					            fieldLabel: '.xml定义', 
					            labelWidth: 30, 
					            msgTarget: 'side', 
					            allowBlank: false, 
					            anchor: '100%', 
					            buttonText: '选择文件' 
							},{ 
					            xtype: 'filefield', 
					            name: 'image', 
					            labelAlign : 'right',
					            fieldLabel: '.png图片', 
					            labelWidth: 30, 
					            msgTarget: 'side', 
					            allowBlank: false, 
					            anchor: '100%', 
					            buttonText: '选择文件' 
							}]
						});
					form.add(panel);
					form.doLayout(true);
			  }
			}, //上传工作流--上传
			"uploadFileForm button[ref=uploadFileButton]" : {
				click : function(btn) {
					  var form =btn.up("uploadFileForm").getForm(); 
			                if(form.isValid()){ 
			                	var grid = Ext.getCmp("deploygrid");
								var store = grid.getStore();
								//校验key是否重复
								form.submit({ 
				                       url : APPBASE + "pc/wfDeploymentAction!isUploadFileByKey.action",
				                        method : "POST",
									    timeout : 4000,
									    waitTitle : '请等待',
				                        waitMsg: '自定义流程key校验中...', 
				                        success: function(response, opts) { 
				                        	var resObj = Ext.decode(response.responseText);
				                        	 Ext.Msg.alert('提示信息', response.obj ); 
				                        	 Ext.Msg.alert('提示信息', opts.obj ); 
				                            Ext.Msg.alert('提示信息', resObj.obj ); 
				                            
				                        } ,
										failure : function(form, action) {
											  Ext.Msg.alert('提示信息', '文件上传失败，请重新上传!'); 
										}
				                    }); 
			                   /* form.submit({ 
			                       url : APPBASE + "pc/wfDeploymentAction!uploadFile.action",
			                        method : "POST",
								    timeout : 4000,
								    waitTitle : '请等待',
			                        waitMsg: '正在上传文件...', 
			                        success: function(fp, o) { 
			                        	store.load();
			                            Ext.Msg.alert('提示信息', '文件上传成功!'); 
			                        } ,
									failure : function(form, action) {
										  Ext.Msg.alert('提示信息', '文件上传失败，请重新上传!'); 
									}
			                    }); */
			                } 
			            } 
			}
		});
	},
	views : ["core.jbpm.view.DeployGrid", "core.jbpm.view.DeployLayout","core.jbpm.view.taskForm.UploadFileForm"],
	stores : ["core.jbpm.store.DeployStore"],
	models : ["core.jbpm.model.DeployModel"]
}); 